package com.cheer.ba.presenter.impl;

import android.content.Context;
import android.widget.ImageView;

import com.cheer.ba.R;
import com.cheer.ba.app.oss.OssManager;
import com.cheer.ba.app.oss.UploadPicManager;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.ReturnCode;
import com.cheer.ba.http.BaseCallback;
import com.cheer.ba.http.CallManager;
import com.cheer.ba.http.HandleRetCode;
import com.cheer.ba.http.Http;
import com.cheer.ba.http.request.APIService;
import com.cheer.ba.model.ResponseData;
import com.cheer.ba.presenter.GeneralPresenter;
import com.cheer.ba.utils.NetUtils;
import com.cheer.ba.view.base.IViewController;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * 处理个人信息
 *
 * @author HQ
 *         created at 2017/4/12 0012 10:00
 */
public class PersonalPresenter extends GeneralPresenter implements UploadPicManager.OnUploadCallback {

    private Context mContext;
    private IViewController mView;
    private APIService mUserService;
    //oss上传
    private UploadPicManager mUpLoadPicManager;

    @Override
    public void attachView(IViewController view) {
        this.mView = view;
        mContext = getContext(view);
        mUserService = Http.getUserService(mContext);
        mUpLoadPicManager = new UploadPicManager(this);
    }

    @Override
    public void detachView() {
    }

    @Override
    public Object getCacheData() {
        return null;
    }

    /**
     * 上传图片
     */
    public void uploadPic(String filePath, ImageView mImgView) {
        /**
         * oss上传图片，得到回调之后，保存图片地址到本地
         */
        UploadPicManager.UploadInfo info = new UploadPicManager.UploadInfo();
        info.fileSavePath = filePath;

        List<UploadPicManager.UploadInfo> dataList = new ArrayList<UploadPicManager.UploadInfo>();
        dataList.add(info);
        mUpLoadPicManager.compressAndUploads(mContext, dataList, OssManager.HEADIMAGE, mImgView);
    }

    @Override
    public void onResult(boolean result, List<UploadPicManager.UploadInfo> resultList) {
        if (result) {
            mView.updateViewWithTag(resultList, ConstantTag.HEADPIC_UP_SUCCESS);
        } else {
            mView.updateViewWithTag(null, ConstantTag.HEADPIC_UP_FAIL);
        }
    }

    /**
     * 保存等信息
     */
    public void savePersonInfo(String uid, final int type, final String paramas) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        Call<ResponseData> call = null;
        switch (type) {
            case IntentCode.Setting.PERSON_UP_PIC:
                call = mUserService.editSaveHeadImg(1050, uid, paramas);
                break;
            case IntentCode.Setting.PERSON_MODIFY_NAME:
                call = mUserService.editSaveNickName(1050, uid, paramas);
                break;
            case IntentCode.Setting.PERSON_MODIFY_SEX:
                call = mUserService.editSaveSex(1050, uid, paramas);
                break;
            case IntentCode.Setting.PERSON_MODIFY_ADDRESS:
                call = mUserService.editSaveLoc(1050, uid, paramas);
                break;
        }
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, type, paramas);
                    } else {
                        HandleRetCode.handler(mContext, res, true, mContext.getString(R.string.modify_login_pw_fail));
                        mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_DATA, type, "");
                    }
                    return;
                }
                mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_DATA, type, "");
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_DATA, type, "");
            }
        });
    }
}
